package top.firejames.io;

import top.firejames.util.FileUtil;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.concurrent.BlockingQueue;

/**
 * @auther 夏浩
 * @creat 2021-09-2021/9/23-21:33
 */
public class LineNumberR {
    public static void LineNumberReadT(String path, int line) throws IOException {
        FileReader fileReader = new FileReader(new File(path));
        LineNumberReader lineNumberReader = new LineNumberReader(fileReader);
        int lineNumber = 0;
        String s = null;
        while ((s = lineNumberReader.readLine()) != null) {
            lineNumber = lineNumberReader.getLineNumber();
            System.out.println("当前行号为" + lineNumber);
            if ((lineNumber == line)) {
                break;
            }
            System.out.println(s);
        }
    }

    /**
     * 从指定的文件中读取指定的行数并且显示在控制台上
     *
     * @param sourcePath 文件路径
     * @param startLine  文件读取的开始行数
     * @param endLine    文件读取的结束行数
     * @throws IOException
     */

    public static void LineNumberRead(String sourcePath, int startLine, int endLine) throws IOException {
        FileReader fileReader = new FileReader(new File(sourcePath));
        LineNumberReader lineNumberReader = new LineNumberReader(fileReader);
        int lineNumber = 0;
        String s = null;
        long allLines = Files.lines(Paths.get(sourcePath)).count();
        while ((s = lineNumberReader.readLine()) != null) {
            lineNumber = lineNumberReader.getLineNumber();
            if (lineNumber >= startLine && lineNumber <= allLines) {
                System.out.println("当前行号为" + lineNumber);
                System.out.println(s);
            }
            if ((lineNumber == endLine)) {
                break;
            }

        }
    }

    public static BlockingQueue LineNumberRead(String sourcePath, BlockingQueue blockingQueue, int startLine, int endLine) throws IOException {
        FileReader fileReader = new FileReader(new File(sourcePath));
        LineNumberReader lineNumberReader = new LineNumberReader(fileReader);
        int lineNumber = 0;
        String s = null;
        long allLines = Files.lines(Paths.get(sourcePath)).count();
        while ((s = lineNumberReader.readLine()) != null) {
            lineNumber = lineNumberReader.getLineNumber();
            if (lineNumber >= startLine && lineNumber <= allLines) {
//                System.out.println("当前行号为" + lineNumber);
//                System.out.println(s);
                blockingQueue.offer(s);
            }
            if ((lineNumber == endLine)) {
                break;
            }

        }
        if (blockingQueue.peek() != null) {
            return blockingQueue;
        } else {
            return null;
        }
    }

    /**
     * @param sourcePath 读文件地址
     * @param destPath   写入文件地址
     * @param startLine  开始读取行
     * @param endLine    读取结束行
     * @throws IOException
     */
    public static void LineNumberRead(String sourcePath, String destPath, int startLine, int endLine) throws IOException {
        FileReader fileReader = new FileReader(new File(sourcePath));
        LineNumberReader lineNumberReader = new LineNumberReader(fileReader);
        int lineNumber = 0;
        String s = null;
        int count = 0;
        long allLines = Files.lines(Paths.get(sourcePath)).count();
        StringBuffer stringBuffer = new StringBuffer(1000);
        while ((s = lineNumberReader.readLine()) != null) {
            lineNumber = lineNumberReader.getLineNumber();
            if (lineNumber >= startLine && lineNumber <= allLines) {
                stringBuffer.append(s).append("\n");
                count++;
                if (count % 1000 == 0) {
                    if (lineNumber == endLine) {
                        stringBuffer.delete(stringBuffer.length() - 1, stringBuffer.length());
                    }
                    FileUtil.write(destPath, stringBuffer.toString());

                    stringBuffer.delete(0, stringBuffer.length());
                }
            }
            if ((lineNumber == endLine)) {
                break;
            }
        }
//        FileUtil.close(destPath);
        System.out.println("一共写入了" + count + "行");
        fileReader.close();
        lineNumberReader.close();
    }
}